﻿using common.consolecomponents;
using common.helpers;
using img.mgr;
using webserver.core;

new ImgMgrHelper("img.mgr.db").Init();

var port = RandomHelper.Instance.Next(5000, 9999);
TaskHelper.RunUseThread(() =>
{
    Init(port);
});

var url = $"http://localhost:{port}";
ProcessStart.Run(url);

Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
Console.WriteLine($"服务已启动，{url}");
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("按任意键退出");
Console.ReadKey();

void Init(int port)
{
    var option = new StartOption
    {
        Assembly = "img.mgr.dll",
        Port = port
    };

    var server = new Server(option);
    server.Runing += Server_Runing;
    server.Start();
}

void Server_Runing(string address)
{
    TaskHelper.RunUseThread(() =>
    {
        while(true)
        {
            try
            {
                new ImgMgrHelper("img.mgr.db").UpdateImgInfo();
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex.ToString());
            }
            Thread.Sleep(120 * 1000);
        }
    });
}